package com.zy.mq;

import com.zy.mq.domain.JMSFactory;

import javax.jms.*;

public class ClientConsumer {


    public static void main(String[] args) throws JMSException{
        //获取连接
        Connection connection=null;
        //获取连接
        try{
            connection=JMSFactory.getConn();
            //开启连接
            connection.start();
            //创建session 会话
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //创建一个队列
            Destination queue = session.createQueue("hello");
            //创建消息消费者
            MessageConsumer consumer = session.createConsumer(queue);

            while (true){
                TextMessage textMessage = (TextMessage) consumer.receive(10000);
                if(textMessage!=null){
                    System.out.println("收到的消息:" + textMessage.getText());
                }else{
                    System.out.println("...................................null..............");
                }
            }
        }catch (JMSException e){
            e.printStackTrace();
        }finally {
            if(connection!=null){
                connection.close();
            }
        }
    }
}
